/*
	Copyright 2007-2010 Petr Ohlidal

	This file is a part of GrobMaker

	GrobMaker is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	GrobMaker is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with GrobMaker.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "InputData.hpp"
#include "Util/FileList.hpp"
#include "Images/Image.hpp"
#include "Util/FileInfo.hpp"

namespace GrobMaker{

bool InputData::checkSpritesResource( const FileInfo& fileInfo ){
	// If the file is a dir, check it's contents
	if( fs::is_directory( fileInfo.status ) ){
		std::list<FileInfo> spriteDirFiles;
		FileList::listDir(fileInfo.entry.path(), spriteDirFiles);
		FileList::removeNonImageFiles(spriteDirFiles);
		// If there's one file with image extension, consider it a single sprite
		if( spriteDirFiles.size()==1 ){
			this->spritePath = spriteDirFiles.front().entry.path();
			return true;
		}
		else{
			FileList::removeUnindexed(spriteDirFiles);
			bool acceptImages=true;
			if(spriteDirFiles.size()>0){
				// If number of angles is known, check image count
				if(this->noAngles!=InputData::noAnglesUnspecified
					&& static_cast<int>(spriteDirFiles.size())<this->noAngles)
				{
					acceptImages=false;
				}
				// If number of angles is unknown, get it.
				else{
					this->noAngles = spriteDirFiles.size();
				}
				if(acceptImages){
					this->spriteFiles.swap( spriteDirFiles );
					this->singleSprite=false;
					this->spritePath=fileInfo.entry.path(); // Path to current directory
					return true;
				}
			}
		}
	}
	// If the file is an image, accept it as single sprite.
	else if(Image::resolveExtension( fileInfo.entry.path().extension() )){
		this->spritePath = fileInfo.entry.path();
		return true;
	}
	return false;
}

} // namespace GrobMaker
